//File: DBConnection.java

package model;

import view.ServerView;
import java.sql.*;

/**
 * Creates a connection to the database.
 * Constructor empty so other classes can access the DBconnection methods.
 * 
 * @author Tobias Robinson
 */
public class DBConnection {    
    private static DBConnection dbc = null;
    private Connection con = null;
    private boolean driverLoaded = false;
    private boolean connectionActive = false;    
    private String host = null;  
     
    private DBConnection() {
       
    }
    /**
     * copies an instance of the DBconnection 
     * 
     * @return  returns instace of DBConnection
     */ 
    public static DBConnection getInstance() {
        
        if (dbc == null) {
            dbc = new DBConnection();
        }
        return dbc;
    } 
    
    /**
     * load mySQL JDBC driver and connects to database    
     *
     * @param host name of host database to connect to
     */
    public boolean connect(String host) {
        String username = "jc163134";
        String password = "mfSOQinA";
        String database = "jc163134db";
        
        try {
            String url = "jdbc:mysql://" + host +  "/" + database;
            
            if (!driverLoaded) {
                DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                driverLoaded = true;
            }
            
            ServerView.printMessage("Connecting to: " + database + "@" + host + " ...\n");
            con = DriverManager.getConnection(url, username, password);
            ServerView.printMessage("Success!\nConnected to: " + database + "@" + host + "\n");
            setConnectionActive();
            return true;
        } catch (SQLException ex) {
            ServerView.printMessage("Could not connect to database, please check arguments\n");
            return false;
            
        }
    }
    
    /**
     * copies the connection 
     * 
     * @return  returns the connection
     */    
    public Connection getConnection() {
        return con;
    }
    
    /**
     * set the connection to active 
     */  
    public void setConnectionActive() {
        connectionActive = true;
    }
    
    /**
     * check if connection is active
     */ 
    public boolean getConnectionActive() {
        return connectionActive;   
    } 
    
    /**
     * saves the connection instance
     * 
     * @param connect connection to create instance of
     */    
    public void setConnection(Connection connect) {
        con = connect;
    }
    
    /**
     * set the database host
     * 
     * @param hostString name of host sent to program
     */    
    public void setHost(String hostString) {
        host = hostString;
    }
    
    /**
     * checks if the connection driver is loaded
     * 
     * @return true if driver is loaded, false otherwise
     */       
    public boolean isDriverLoaded() {
        return driverLoaded;
    }
    public void closeConnection() {
        try {
            con.close();
        } catch (Exception e) {
            ServerView.printMessage("Error: " + e);
        }
    }
    
    /**
     * cleanup, called by garbage collector    
     */  
    protected void finalize() throws Throwable {
        if (con != null) {
            con.close();
        }
        
        //finalize() must call super as the last thing it does        
        super.finalize();
    }
}